﻿using System;
using System.Diagnostics.CodeAnalysis;

using Fuse8.DomainFramework.Domain;
using Fuse8.DomainFramework.Common.Locator;

namespace Fuse8.DomainFramework.DataAccess.InMemoryDatabase
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <typeparam name="TKey"></typeparam>
    public static class InMemoryDataRepositoryConfiguration<TEntity, TKey> 
        where TEntity : class, IEntityObject<TKey>, new()
        where TKey : IComparable<TKey>, IEquatable<TKey> 
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes",
            Justification = "This is convenient way to call it")]
        public static IEntityDataRepository<TEntity, TKey> CreateRepository()
        {
            if (!DependencyServiceLocator.Current.IsDefaultServiceRegistered<IUnitOfWorkFactory<TEntity>>())
            {
                DependencyServiceLocator.Current.SetDefaultService<IUnitOfWorkFactory<TEntity>>(
                    CreateUnitOfWorkFactory());
            }

            return new InMemoryDataRepository<TEntity, TKey>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes",
            Justification = "This is convenient way to call it")]
        internal static IEntityDataRepositoryMapper<TEntity, TEntity> CreateMapper()
        {
            return new InMemoryDataRepositoryMapper<TEntity, TKey>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes",
            Justification = "This is convenient way to call it")]
        internal static IUnitOfWorkFactory<TEntity> CreateUnitOfWorkFactory()
        {
            return new StubUnitOfWorkFactory<TEntity>();
        }
    }
}
